package com.jrx.anytxn.reporting.job.payment.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.bean.PaymentDataBean;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.payment.step.AutoAssemPaymentLogProcessor;
import com.jrx.anytxn.reporting.job.payment.step.AutoAssemPaymentLogReader;
import com.jrx.anytxn.reporting.job.payment.step.AutoAssemPaymentLogWriter;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;


/**
 * @author ：lbz
 * @date ：2020/1/6
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class AutoAssemPaymentLogConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(AutoAssemPaymentLogConfiguration.class);


    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private AutoAssemPaymentLogProcessor autoAssemPaymentLogProcessor;
    @Autowired
    private AutoAssemPaymentLogWriter autoAssemPaymentLogWriter;

    @Bean
    public Job autoAssemPaymentLogJob(){
        return jobBuilderFactory.get("autoAssemPaymentLogJob")
                .start(autoAssemPaymentLogJobStep())
                .build();
    }

    @Bean
    public Step autoAssemPaymentLogJobStep(){
        return stepBuilderFactory.get("autoAssemPaymentLogJobStep")
                .<TlPaymentLog, PaymentDataBean>chunk(anyTxnBatchProperties.getReportingPaymentConfig().getChunkLimit())
                .reader(autoAssemPaymentLogReader())
                .processor(autoAssemPaymentLogProcessor)
                .writer(autoAssemPaymentLogWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingPaymentConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> autoAssemPaymentLogReader(){
        logger.info("抽取还款流水批量启动,机构：{}",ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        return new AutoAssemPaymentLogReader(sqlSessionFactory,anyTxnBatchProperties.getReportingPaymentConfig().getPageSize(),org);
    }

}
